package com.rzjm.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDAO;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private UserShopRelationDao userShopRelationDao;

    @Override
    public List<User> selectByShopId(Integer shop_id, Integer user_id, Integer offset, Integer pagesize, Date begin_time, Date end_time) {
        return userDAO.selectByShopId(shop_id,user_id,offset,pagesize,begin_time,end_time);
    }

    @Override
    public int bandShop(User user,SysUser sysUser) throws TransactionalException{
        try{
            int ret = userDAO.updateById(user);
            if(ret <= 0){
                throw new TransactionalException("userDAO  update error");
            }

            if(sysUser != null){
                if(sysUser.getUserType() >= 4 && sysUser.getUserType() <= 7){
                    ret = sysUserDao.updateById(sysUser);
                    if(ret <= 0){
                        throw new TransactionalException("sysUserDao  update error");
                    }
                }else if(sysUser.getUserType() >= 2 && sysUser.getUserType() <= 3){

                    QueryWrapper<UserShopRelation> qw = new QueryWrapper<>();
                    qw.eq("sys_user_id", sysUser.getUserId());
                    qw.eq("shop_id", user.getShopId());
                    qw.last("limit 1");
                    UserShopRelation shopRelation = userShopRelationDao.selectOne(qw);
                    if(shopRelation == null){
                        shopRelation = new UserShopRelation();
                        shopRelation.setJituanUserId(sysUser.getJituanId());
                        shopRelation.setShopId(user.getShopId());
                        shopRelation.setSysUserId(sysUser.getUserId());
                        ret = userShopRelationDao.insert(shopRelation);
                        if(ret <= 0){
                            throw new TransactionalException("userShopRelationDao  insert error");
                        }
                    }
                }
            }
            return ret;
        }catch (TransactionalException ex){
            throw new TransactionalException("bandShop system error");
        }
    }

    @Override
    public int unBandShop(User user , SysUser sysUser,int shopId) throws TransactionalException{
        try{
            user.setShopId(0);
            int ret = userDAO.updateById(user);
            if(ret <= 0){
                throw new TransactionalException("userDAO  update error");
            }
            if(sysUser != null){
                if(sysUser.getUserType() >= 4 && sysUser.getUserType() <= 7){
                    sysUser.setShopId(0);
                    ret = sysUserDao.updateById(sysUser);
                    if(ret <= 0){
                        throw new TransactionalException("sysUserDao  update error");
                    }
                }else if(sysUser.getUserType() >= 2 && sysUser.getUserType() <= 3){
                    QueryWrapper<UserShopRelation> qw = new QueryWrapper<>();
                    qw.eq("sys_user_id", sysUser.getUserId());
                    qw.eq("shop_id", shopId);
                    qw.last("limit 1");
                    UserShopRelation shopRelation = userShopRelationDao.selectOne(qw);
                    if(shopRelation!=null){
                        ret = userShopRelationDao.deleteById(shopRelation.getId());
                        if(ret <= 0){
                            throw new TransactionalException("userShopRelationDao  deleteById error");
                        }
                    }
                }
            }
            return ret;
        }catch (TransactionalException ex){
            throw new TransactionalException("bandShop system error");
        }
    }

    @Override
    public List<User> getUserListBySysUserId(Integer sysUserId,String platform) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("sys_user_id",sysUserId );
        qw.eq("platform",platform);
        return userDAO.selectList(qw);
    }
}
